package com.smzd.framework.sso;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;

import com.smzd.framework.system.domain.User;

public class XtwlAuthenticationEntryPoint implements AuthenticationEntryPoint {
    private String loginUrl;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException)
            throws IOException, ServletException {
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("_remote_user_");
        String url;
        if (user != null) {// 这是要绑定用户了
            String base = request.getContextPath();
            if (base == null || base.length() == 1) {
                base = "";
            }
            url = base + "/workspace/login";
        } else {
            url = generateOuterLoginRedirectURL();
        }
        response.sendRedirect(url);
    }

    protected String generateOuterLoginRedirectURL() {
        return loginUrl;
    }

    public String getLoginUrl() {
        return loginUrl;
    }

    public void setLoginUrl(String loginUrl) {
        this.loginUrl = loginUrl;
    }

}
